'''

Case Type： 数据库系统
Case Name： 将pg_log目录权限删除，不影响业务正常运行
Create At:  2020/06/23
@zou_jialiang0505328126
Descption:  1.执行TPCC事务2.执行TPCC业务过程中删除pg_log权限3.查看TPCC业务是否被影响

history：
created： 2020/06/23  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
import _thread
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.ComThread import *
from testcase.utils.Constant import *


class ModifyPglogPermi(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case041.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.PG_LOG_PATH = macro.PG_LOG_PATH
        self.TPCC_PATH = macro.TPCC_PATH
        self.com = Common()
        self.Constant = Constant()

    def test_modify_pglog_permission(self):
        self.log.info('----------------------------开始执行tpcc-----------------------------')
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.rootNode, self.TPCC_PATH,))
        tpcc_thread.start()
        time.sleep(20)

        # delete pg_log permission
        self.log.info("delete pg_log permission")
        modfiy_permis_cmd = f'chmod -R 000 {self.PG_LOG_PATH}'
        self.log.info(modfiy_permis_cmd)
        modify_msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.assertEqual(modify_msg, '')
        show_permission_cmd = f'ls -al {self.PG_LOG_PATH}'
        self.log.info(show_permission_cmd)
        show_msg = self.rootNode.sh(show_permission_cmd).result()
        self.log.info(show_msg)

        tpcc_thread.join(timeout=int(macro.TPCC_RUN_MAX_TIME) * 2)

        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

    def tearDown(self):
        self.log.info('this is tearDown')
        # recover pg_log permission
        self.log.info('recover pg_log permission')
        modfiy_permis_cmd = 'chmod -R 700 ' + self.PG_LOG_PATH
        self.log.info(modfiy_permis_cmd)
        msg = self.rootNode.sh(modfiy_permis_cmd).result()
        self.log.info(msg)
